package cz.idsv.web.mvc.controller;

/**
 * PersonType Controller.
 * 
 * @author Jan Borzensky
 */
import java.util.Collection;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;

import cz.idsv.web.mvc.model.entity.PersonTypeEntity;
import cz.idsv.web.mvc.model.service.PersonTypeService;



/**
 * PersonType controller.
 * 
 * @author Jan Borzensky
 */
@Controller
@SessionAttributes(value = {"personTypeEntity"})
public class PersonTypeController {
	
	private static final String LIST_MODEL_KEY = "list";
	private static final String PERSONTYPE_ENTITY = "personTypeEntity";
	
	protected static Logger logger = Logger.getLogger("PersonTypeController"); 
	
	@Autowired()
	private PersonTypeService personTypeService;
	
	/**
     * <p>Searches for all product and returns them in a 
     * <code>Collection</code>.</p>
     * 
     * <p>Expected HTTP GET and request '/personType/list'.</p>
     */
    @RequestMapping(value="/personType/list", method=RequestMethod.GET)
    public @ModelAttribute(LIST_MODEL_KEY) Collection<PersonTypeEntity> search() {
    	logger.debug("Received request to show all product"); 
    	
        return personTypeService.findPersonType();
    }
    
	/**
     * <p>Create new or update old personType 
     * <code>PersonTypeEntity</code>.</p>
     * 
     * <p>Expected HTTP GET and request '/personType/form'.</p>
     */
    @RequestMapping(value="/personType/form", method=RequestMethod.GET)
    public @ModelAttribute(PERSONTYPE_ENTITY)  PersonTypeEntity getDocumentForm(@RequestParam(value = "id", required = false) Integer id) {
        if(id==null)return new PersonTypeEntity();
        return personTypeService.findPersonTypeById(id);
    }
    
	/**
     * <p>Save new or update old personType 
     * <code>AddressEntity</code>.</p>
     * 
     * <p>Expected HTTP POST and response '/personType/saveForm'.</p>
     */
    @RequestMapping(value="/personType/saveForm", method=RequestMethod.POST)
    public String savePersonTypeForm(PersonTypeEntity personTypeEntity, Model model) {
    	if(personTypeEntity != null) 
    		personTypeService.save(personTypeEntity);
    	return "redirect:/personType/list.html";
    }
    
    /**
     * <p>Deletes a personType.</p>
     * 
     * <p>Expected HTTP POST and request '/personType/delete'.</p>
     */
    @RequestMapping(value="/personType/delete", method=RequestMethod.POST)
    public String delete(PersonTypeEntity personTypeEntity) {
    	logger.debug("Received request to delete address with id:"+ personTypeEntity.getId());
        personTypeService.delete(personTypeEntity);
        return "redirect:/personType/list.html";
    }
    
}
